package org.mercworks.knight.models.characters;

//import java.net.URL;
//import java.nio.FloatBuffer;
import java.util.List;

import org.lwjgl.opengl.GL11;
import org.mercworks.knight.animation.Animatable;
import org.mercworks.knight.physics.bodies.KnightBody;
//import org.mercworks.knight.textures.KnightTexture;
//import org.mercworks.knight.util.AllocationUtil;
//import org.mercworks.knight.util.TextureUtils;
import org.mercworks.knight.util.modelImporter.ImportModelUtil;
import org.mercworks.knightgame.model.KnightGameModel;
import org.mercworks.knightgame.model.dvo.Animation;
import org.mercworks.knightgame.model.dvo.Face;
import org.mercworks.knightgame.model.dvo.Vertex;

public class Knight implements Animatable
{
	private KnightGameModel knightModel;
	
	private float xPos;
	private float yPos;
	private float zPos;
	
	private float xRot;
	private float yRot;
	private float zRot;
	
	private List<Vertex> listVertices;
	private List<Face> listFaces;
	private List<Animation> listAnimations;
	
	//private KnightTexture m_knightTexture;
	//private static final String TEXTURE_BASE_PATH = "/images/";
	private KnightBody m_knightBody;
	
	public Knight()
	{
		ImportModelUtil importModelUtil = new ImportModelUtil();
		knightModel = importModelUtil.importModelFromFile("/models/box.kgm");
		
		listVertices = knightModel.getListVertices();
		listFaces = knightModel.getListFaces();
		listAnimations = knightModel.getListAnimations();
		
		/* Position movement initializers */
		xPos = 0.0f;
		yPos = 0.0f;
		zPos = 0.0f;
		
		/* Rotation movement initializers */
		xRot = 0.0f;
		yRot = 0.0f;
		zRot = 0.0f;
		
		//m_strTextureName = TEXTURE_BASE_PATH + "headskin.jpg";

		//URL urlTexture = this.getClass().getResource(m_strTextureName);
		
		/*try
		{
			m_knightTexture = new KnightTexture(urlTexture);
			TextureUtils.makeTexture(m_knightTexture);
			TextureUtils.makeTextureMipMap(m_knightTexture);
		}
		catch(Exception ex)
		{
			System.out.println("Texture Not Found.  Continuing without a texture");
		}
		
		m_knightBody = new KnightBody();
		m_x = 0.0f;
		m_y = 0.0f;
		
		m_direction = 0; */
	}
	
	public float getXPos()
	{
		return xPos;
	}
	
	public float getYPos()
	{
		return yPos;
	}
	
	public float getZPos()
	{
		return zPos;
	}
	
	public float getXRot()
	{
		return xRot;
	}
	
	public float getYRot()
	{
		return yRot;
	}
	
	public float getZRot()
	{
		return zRot;
	}
	
	public void setDx(float dx)
	{
		xPos += dx;
	}
	
	public void setDy(float dy)
	{
		yPos += dy;
	}
	
	public void setDz(float dz)
	{
		zPos += dz;
	}
	
	public void setRotDx(float rotDx)
	{
		xRot += rotDx;
	}
	
	public void setRotDy(float rotDy)
	{
		yRot += rotDy;
	}
	
	public void setRotDz(float rotDz)
	{
		zRot += rotDz;
	}
	
	public List<Animation> getListAnimations()
	{
		return listAnimations;
	}
	
	public void setListAnimations(List<Animation> listAnimations)
	{
		this.listAnimations = listAnimations;
	}
	
	public void render()
	{
		//FloatBuffer ambientFloatBuffer = AllocationUtil.allocFloats(m_ambient);
		//FloatBuffer specularFloatBuffer = AllocationUtil.allocFloats(m_specular);
		//FloatBuffer diffuseFloatBuffer = AllocationUtil.allocFloats(m_diffuse);
		
		//GL11.glEnable(GL11.GL_COLOR_MATERIAL);
		//GL11.glColorMaterial(GL11.GL_FRONT, GL11.GL_AMBIENT_AND_DIFFUSE);
		
		//GL11.glMaterial(GL11.GL_FRONT, GL11.GL_AMBIENT, ambientFloatBuffer);
		//GL11.glMaterial(GL11.GL_FRONT, GL11.GL_SPECULAR, specularFloatBuffer);
		//GL11.glMaterial(GL11.GL_FRONT, GL11.GL_DIFFUSE, diffuseFloatBuffer);
		
		float x = 0.0f;
		float y = 0.0f;
		float z = 0.0f;
		
		//GL11.glEnable(GL11.GL_CULL_FACE);
		//GL11.glCullFace(GL11.GL_BACK);
		
		int vertIndex = listFaces.size();
		
		GL11.glBegin(GL11.GL_TRIANGLES);
			for(int i = 0; i < vertIndex; i++)
			{
				//x = m_listTvert.get(m_listTfaces.get(i).getTfaceA()).getX();
				//y = m_listTvert.get(m_listTfaces.get(i).getTfaceA()).getY();
				//GL11.glTexCoord2f(x, y);
				
				x = listVertices.get(listFaces.get(i).getFaceA()).getX();
				y = listVertices.get(listFaces.get(i).getFaceA()).getY();
				z = listVertices.get(listFaces.get(i).getFaceA()).getZ();
				GL11.glVertex3f(x, y, z);
				
				//x = m_listTvert.get(m_listTfaces.get(i).getTfaceB()).getX();
				//y = m_listTvert.get(m_listTfaces.get(i).getTfaceB()).getY();
				//GL11.glTexCoord2f(x, y);
				
				x = listVertices.get(listFaces.get(i).getFaceB()).getX();
				y = listVertices.get(listFaces.get(i).getFaceB()).getY();
				z = listVertices.get(listFaces.get(i).getFaceB()).getZ();
				GL11.glVertex3f(x, y, z);
				
				//x = m_listTvert.get(m_listTfaces.get(i).getTfaceC()).getX();
				//y = m_listTvert.get(m_listTfaces.get(i).getTfaceC()).getY();
				//GL11.glTexCoord2f(x, y);
				
				x = listVertices.get(listFaces.get(i).getFaceC()).getX();
				y = listVertices.get(listFaces.get(i).getFaceC()).getY();
				z = listVertices.get(listFaces.get(i).getFaceC()).getZ();
				GL11.glVertex3f(x, y, z);
			}
		GL11.glEnd();
	}
	
	public void cleanUp()
	{
		m_knightBody.deleteFromWorld();
	}
}
